#!/usr/bin/env python
# encoding: utf-8


"""
@file: triangle_prove_actions.py
@time: 2017/5/15 上午9:43
"""
# 三角形证明的 action函数

from mathsolver.functions.base import *
from sympy import sin, cos, symbols, simplify, expand_trig
from mathsolver.functions.budengshi import common_opers as co
from itertools import chain
from mathsolver.functions.sanjiao import sanjiaoxing_utils as sjx_u
from mathsolver.functions.sanjiao.triangle_structs import ActionFunc


# 只包含边角关系的 可以假设某一条边的值
def _one_cond_action_1(cond_eqs):
    action_results = {}
    af = ActionFunc()
    cond_eq_list = cond_eqs.sympify()
    cond_f_list = map(lambda pair: pair[0] - pair[1], cond_eq_list)
    _a, _b, _c = symbols('a,b,c')
    flag = True
    _eqs = []
    for _f in cond_f_list:
        symbs = list(_f.free_symbols)
        if len(symbs) == 1 and symbs[0] in [_a, _b, _c]:
            flag = False
            break
        if len(cond_eq_list) == 1:
            af.add_step(['\because ' + BaseEq(cond_eq_list[0]).printing(), ''])
        else:
            af.add_step(['\because ' + cond_eqs.printing(), ''])
        _f = expand_trig(_f)
        _f2 = _f.subs(sin('A'), 'a/(2*r)').subs(cos('A'), '(b**2 + c**2 - a**2)/(2*b*c)')
        if _f2 != _f:
            af.add_step(['\because ' + BaseEq([_f, '0']).printing(), '由正弦余弦定理得:' + BaseEq([_f2]).printing()])
            _eqs.append(['((b**2 + c**2 - a**2)/(2*b*c))**2 + (a/(2*r))**2', '1'])
        _f3 = _f2.subs(sin('B'), 'b/(2*r)').subs(cos('B'), '(a**2 + c**2 - b**2)/(2*a*c)')
        if _f3 != _f2:
            af.add_step(['\because ' + BaseEq([_f2, '0']).printing(), '由正弦余弦定理得:' + BaseEq([_f3]).printing()])
            _eqs.append(['((a**2 + c**2 - b**2)/(2*a*c))**2 + (b/(2*r))**2', '1'])
        _f4 = _f3.subs(sin('C'), 'c/(2*r)').subs(cos('C'), '(a**2 + b**2 - c**2)/(2*a*b)')
        if _f4 != _f3:
            af.add_step(['\because ' + BaseEq([_f3, '0']).printing(), '由正弦余弦定理得:' + BaseEq([_f4]).printing()])
            _eqs.append(['((a**2 + b**2 - c**2)/(2*a*b))**2 + (c/(2*r))**2', '1'])
        _eqs.append([_f4, '0'])
    af.add_label('正弦定理')
    af.add_label('余弦定理')
    if flag:  # 如果可以，那么设其中一条边的值为1
        assuem_eq = ['b', '1']
        extend_eqs = list(chain(_eqs, [assuem_eq]))
        print_eqs = BaseEqs(_eqs)
        af.add_step(['得到方程组:', print_eqs.printing()])
        abc_list = co.isolve_eqs(BaseEqs(extend_eqs), [_a, _b, _c])
        # 合法的边
        abc_list = filter(
            lambda _: sjx_u.legal_sided(_[0][1]) and sjx_u.legal_sided(_[1][1]) and sjx_u.legal_sided(
                _[2][1]), abc_list)
        # 是否满足两边之和大于第三边
        abc_list = list(filter(lambda _: sjx_u.legal_sides((_[0][1], _[1][1], _[2][1])), abc_list))
        a_v, b_v, c_v = map(simplify, zip(*abc_list[0])[1])
        print_eqs = BaseEqs([['a', a_v * _b], ['c', c_v * _b]])
        af.add_step(['验证求得:', print_eqs.printing()])
        action_results[_a] = a_v * _b
        action_results[_c] = c_v * _b
        return action_results, af
    else:
        raise Exception('Type Match Error')


one_cond_actions = [_one_cond_action_1]
